package zzs.generalframework.console.core.user.userbaseinner.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.user.userbaseinner.UserBaseInnerBO;
import zzs.generalframework.common.core.user.userbaseinner.UserBaseInnerInfo;
import zzs.generalframework.common.core.user.userbaseinner.UserBaseInnerPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.user.userbaseinner.dao.IUserBaseInnerDao;
import zzs.generalframework.console.core.user.userbaseinner.service.IUserBaseInnerService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.userbaseinner.service.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.user.userbaseinner.service.impl.userBaseInnerServiceImpl")
public class UserBaseInnerServiceImpl implements IUserBaseInnerService
{

	private Logger logger = LoggerFactory.getLogger(UserBaseInnerServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IUserBaseInnerDao userBaseInnerDao;

	@Override
	public ConsoleReturnBO saveUserBaseInner(UserBaseInnerBO userBaseInnerBO)
	{
		logger.debug("====保存用户基本信息开始====");
		logger.debug("====保存用户基本信息.入参:UserBaseInnerBO=[{}]====", userBaseInnerBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserBaseInnerPO userBaseInnerPO = new UserBaseInnerPO();
		copyUserBaseInnerBO2UserBaseInnerPO(userBaseInnerBO, userBaseInnerPO);
		Boolean insertResult = userBaseInnerDao.insert(userBaseInnerPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存用户基本信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户基本信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveUserBaseInner(List<UserBaseInnerBO> userBaseInnerBOList) throws ConsoleException
	{
		logger.info("====保存用户基本信息列表开始====");
		logger.debug("====保存用户基本信息列表.入参:UserBaseInnerBOList=[{}]====", userBaseInnerBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserBaseInnerBO itemBO : userBaseInnerBOList)
		{
			ConsoleReturnBO itemResult = saveUserBaseInner(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====保存用户基本信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存用户基本信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getUserBaseInner(UserBaseInnerInfo userBaseInnerInfo)
	{
		logger.info("====获取用户基本信息开始====");
		logger.debug("====获取用户基本信息.入参:UserBaseInnerInfo=[{}]====", userBaseInnerInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserBaseInnerPO> userBaseInnerPOList = userBaseInnerDao.select(userBaseInnerInfo);
		if (userBaseInnerPOList == null || userBaseInnerPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (userBaseInnerPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			UserBaseInnerBO userBaseInnerBO = new UserBaseInnerBO();
			copyUserBaseInnerPO2UserBaseInnerBO(userBaseInnerPOList.get(0), userBaseInnerBO);
			returnConsoleReturnBO.setReturnObj(userBaseInnerBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			UserBaseInnerBO userBaseInnerBO = new UserBaseInnerBO();
			copyUserBaseInnerPO2UserBaseInnerBO(userBaseInnerPOList.get(0), userBaseInnerBO);
			returnConsoleReturnBO.setReturnObj(userBaseInnerBO);
		}
		logger.debug("====获取用户基本信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户基本信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listUserBaseInner(UserBaseInnerInfo userBaseInnerInfo)
	{
		logger.info("====获取用户基本信息列表开始====");
		logger.debug("====获取用户基本信息列表.入参:UserBaseInnerInfo=[{}]====", userBaseInnerInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<UserBaseInnerPO> userBaseInnerPOList = userBaseInnerDao.select(userBaseInnerInfo);
		if (userBaseInnerPOList == null || userBaseInnerPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<UserBaseInnerBO> resultList = new ArrayList<UserBaseInnerBO>();
			for (UserBaseInnerPO itemPO : userBaseInnerPOList)
			{
				UserBaseInnerBO itemBO = new UserBaseInnerBO();
				copyUserBaseInnerPO2UserBaseInnerBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取用户基本信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取用户基本信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserBaseInner(UserBaseInnerBO userBaseInnerBO)
	{
		logger.info("====更新用户基本信息开始====");
		logger.debug("====更新用户基本信息.入参:UserBaseInnerBO=[{}]====", userBaseInnerBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserBaseInnerPO userBaseInnerPO = new UserBaseInnerPO();
		copyUserBaseInnerBO2UserBaseInnerPO(userBaseInnerBO, userBaseInnerPO);
		Boolean updateResult = userBaseInnerDao.update(userBaseInnerPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新用户基本信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户基本信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateUserBaseInner(List<UserBaseInnerBO> userBaseInnerBOList) throws ConsoleException
	{
		logger.info("====更新用户基本信息列表开始====");
		logger.debug("====更新用户基本信息列表.入参:List<UserBaseInnerBO>=[{}]====", userBaseInnerBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserBaseInnerBO itemBO : userBaseInnerBOList)
		{
			ConsoleReturnBO itemResult = updateUserBaseInner(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====更新用户基本信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新用户基本信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserBaseInner(UserBaseInnerBO userBaseInnerBO)
	{
		logger.info("====删除用户基本信息开始====");
		logger.debug("====删除用户基本信息.入参:UserBaseInnerBO＝[{}]======", userBaseInnerBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		UserBaseInnerPO userBaseInnerPO = new UserBaseInnerPO();
		copyUserBaseInnerBO2UserBaseInnerPO(userBaseInnerBO, userBaseInnerPO);
		Boolean deleteResult = userBaseInnerDao.delete(userBaseInnerPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除用户基本信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户基本信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeUserBaseInner(List<UserBaseInnerBO> userBaseInnerBOList) throws ConsoleException
	{
		logger.info("====删除用户基本信息列表开始====");
		logger.debug("====删除用户基本信息列表.入参:List<UserBaseInnerBO>＝[{}]======", userBaseInnerBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (UserBaseInnerBO itemBO : userBaseInnerBOList)
		{
			ConsoleReturnBO itemResult = removeUserBaseInner(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException(itemResult.getMessage());
			}
		}
		logger.debug("====删除用户基本信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除用户基本信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countUserBaseInner(UserBaseInnerBO userBaseInnerBO)
	{
		logger.info("====统计用户基本信息开始====");
		logger.debug("====统计用户基本信息。入参：UserBaseInnerBO＝[{}]======", userBaseInnerBO);
		Long returnLong = new Long(0);
		UserBaseInnerPO userBaseInnerPO = new UserBaseInnerPO();
		copyUserBaseInnerBO2UserBaseInnerPO(userBaseInnerBO, userBaseInnerPO);
		returnLong = userBaseInnerDao.count(userBaseInnerPO);
		logger.debug("====统计用户基本信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计用户基本信息结束====");
		return returnLong;
	}

	private void copyUserBaseInnerBO2UserBaseInnerPO(UserBaseInnerBO userBaseInnerBO, UserBaseInnerPO userBaseInnerPO)
	{
		beanUtils.copyBeanNotNull(userBaseInnerBO, userBaseInnerPO);
		if (userBaseInnerBO.getSysID() != null)
		{
			userBaseInnerPO.setSeqID(userBaseInnerBO.getSysID());
		}
	}

	private void copyUserBaseInnerPO2UserBaseInnerBO(UserBaseInnerPO userBaseInnerPO, UserBaseInnerBO userBaseInnerBO)
	{
		beanUtils.copyBeanNotNull(userBaseInnerPO, userBaseInnerBO);
		if (userBaseInnerPO.getSeqID() != null)
		{
			userBaseInnerBO.setSysID(userBaseInnerPO.getSeqID());
		}
	}

}
